#!/usr/bin/python
# -*- coding: UTF-8 -*-
import pymongo;
from  config import *
# browser=webdriver.PhantomJS(service_args=SERVICE_ARGS);
client = pymongo.MongoClient(MONGO_URL);
db = client["xiecheng"];
TABLE="hangzhou"
import pandas as pd
import re
import numpy as np
import matplotlib.pyplot as plot
#指定默认字体
plot.rcParams['font.sans-serif'] = ['SimHei']
plot.rcParams['font.family']='sans-serif'
def get_data():
    queryArgs = {}
    projectionFields = {'_id': False}  # 用字典指定
    result = db[TABLE].find(queryArgs,projectionFields)
    return result


def main():
    data=pd.DataFrame(list(get_data()))
    print(data.describe())

    # data["com_count"]=data["com_count"].apply(filter_cmd)
    # data["com_count"]=data["com_count"].astype(np.int16)
    # data["price"]=data["price"].astype(np.float)
    # data["score"]=data["score"].astype(np.float)
    # data=data.sort_values(by="com_count",ascending=False)
    # data=data[data.com_count >1000]
    # data["com_count"]=data["com_count"]/1000
    # data=data.sort_values(by="score",ascending=False)
    # data=data.head(30)
    # data=data.sort_values(by="price",ascending=True)
    # data=data.head(10)
    # price(data)

    # plot.style.use("ggplot")
    # show_score(data)



    # show_com(data)





    # print(data.head())
    # data = data.sort_values(by="com_count", ascending=False)
    # print(data.head())
    # data=data.sort_values(by="score",ascending=False)
    # print(data.head(5))


def price(data):
    plot.style.use("ggplot")
    bar_width = 0.3
    plot.figure(figsize=(14, 6))
    plot.title("综合评论和评分价格最便宜的景点")
    plot.xlabel("价格 单元(/元)")
    p2 = plot.bar(left=0, bottom=data["title"], width=data["price"], height=0.5, orientation='horizontal')
    plot.savefig("xjb.png")
    plot.show()


def show_score(data):
    bar_width = 0.3
    plot.figure(figsize=(14, 6))
    plot.title("评分最高的10个景点")
    plot.ylabel("评论次数 单位(k) 评分 单位为(分)")
    t1 = plot.bar(left=data["title"], height=data["score"], width=bar_width, color="r")
    t2 = plot.bar(left=data["title"], height=data["com_count"], width=bar_width, color="b", bottom=data["score"])
    # p2 = plot.bar(left=0, bottom=data["title"], width=data["score"], height=0.5, orientation='horizontal')
    plot.legend(handles=[t1, t2, ], labels=['评分', '评论次数'], loc='best')
    plot.savefig("score.png")
    plot.show()


def show_com(data):
    plot.style.use("seaborn-bright")
    bar_width = 0.3
    plot.figure(figsize=(14, 6))
    plot.title("杭州市评论次数最多的10个景点")
    p2 = plot.bar(left=0, bottom=data["title"], width=data["com_count"], height=0.5, orientation='horizontal')
    plot.savefig("com.png")
    plot.show()


def count_cmd(data):
    pass





def filter_cmd(str):
    return re.compile("\d+").search(str).group(0)


if __name__ == '__main__':
    main()